<?php
header('Content-Type: text/html; charset=utf-8');

define('SITE_DIR',dirname(__FILE__));
require_once dirname(__FILE__) . '/../projects/framework/core/include.inc';

//using('Framework.System.Autoload');
using('Framework.System.Config');
using('Framework.System.Error');
using('Framework.System.Session');
using('Framework.System.Routes');
using('Framework.System.Error.Libs.Log4php');
using('Framework.System.View');
using('Framework.System.ORM');

require_once 'code/basecontroller.class.inc';
require_once SITE_DIR . '/code/models/baseclasses/baserecord.class.inc';

//echo SVNRepository::isValidRepository('/usr') ? 'true' : 'false';
//SVNRepository::createRepository('/var/svn-repos/svn/buvr2');
//SVNRepository::createStandardDirectories('/var/svn-repos/svn/buvr2')

class Main
{
    private static $config;

    private static $route;

    /**
     * Видаляє слешування якщо це потрібно
     */
    public static function removeSlashes()
    {
        if ((function_exists('get_magic_quotes_gpc') && 
             get_magic_quotes_gpc()) || ini_get('magic_quotes_sybase')) {

            $_POST = array_map('stripslashes', $_POST);
            $_GET = array_map('stripslashes', $_GET);
            $_COOKIE = array_map('stripslashes', $_COOKIE);
        }
    }

    public static function loadConfig()
    {
        self::$route = new Router(array(
                        'urlRewriting' => true,
                        'folder' => SITE_DIR . '/code/controllers'));

        self::initRoutes();
    }

    public static function initRoutes()
    {
        self::$route->add('dashboard', 'dashboard', array('action' => 'dashboard'));
        self::$route->add('project_view', 'projects/:action/:name', array('controller' => 'projects') );
        self::$route->add('project_search', 'project/:project/search', array('controller' => 'projects','action' => 'search') );

        # docs
        self::$route->add('docs_default', 'project/:project/:controller/:action');
        self::$route->add('docs_index',   'project/:project/:controller');
        self::$route->add('docs_package', 'project/:project/:controller/package/:package', array('action' => 'packageView') );
        self::$route->add('docs_file',    'project/:project/:controller/file/*path', array('action' => 'pathView') );
        self::$route->add('docs_class',   'project/:project/:controller/class/:className', array('action' => 'showclass') );

        #wiki
        self::$route->add('wiki_default', 'wiki/edit', array('controller' => 'wiki'));
        self::$route->add('wiki_newdefault', 'wiki/new', array('controller' => 'wiki', 'action' => 'new'));
        self::$route->add('wiki_edit', 'wiki/:action/*path', array('controller' => 'wiki'));

        #user
        self::$route->add('user_profile', 'users/:login', array('controller' => 'user', 'action' => 'profile'));

        self::$route->add('default3', ':controller/:action/:id' );
        self::$route->add('default2', ':controller/:action' );
        self::$route->add('default1', ':controller' );
    }

    public static function start()
    {
        self::$config = WebConfig::load('web.config');
        self::removeSlashes();
        self::loadConfig();
        
        //try {
            View::Singleton()->assign('url', Url::getRequestUrl());
            View::Singleton()->assign('route', self::$route);

            self::$route->dispatch(trim(Url::getRequestUrl(), '/'));
        /*} catch (RouteMapNotFoundException $e) {
            exit('Error: Route not found');
        } catch (RouteMapNoMatchException $e) {
            exit('Error: Page not found');
        } catch (RouteMapReqsException $e) {
            exit('Error: Invalid URL arguments');
        } catch (RouteMapNoReqsException $e) {
            exit('Error: Ambiguous URL rules detected');
        } catch(Exception $e) {
            print '<pre>';
            print $e->getMessage()."/n";
            print_r($e->getTraceAsString());
            print '</pre>';
        }*/
    }
}

Main::start();
